#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

class Student
{
public:
    Student(int id, int age, const char * name)
    : _id(id)
    , _age(age)
    , _name(new char[strlen(name) + 1]())
    {
        strcpy(_name, name);
        cout << "Student(int,int,const char *)" << endl;
    }

    ~Student()
    {
        cout << "~Student()" << endl;
        if(_name){
            delete [] _name;
            _name = nullptr;
        }
    }

    Student(const Student & rhs)
    : _id(rhs._id)
    , _age(rhs._age)
    , _name(new char[strlen(rhs._name) + 1]())
    {
        strcpy(_name,rhs._name);
        cout << "Student(const Student &)" << endl;
    }
    
    Student & operator=(const Student & rhs){
        cout << "Student & operator=(const Student &)" << endl;
        if(this != &rhs){
            delete [] _name;
            _name = new char[strlen(rhs._name) + 1]();
            strcpy(_name,rhs._name);
            _id = rhs._id;
            _age = rhs._age;
        }
        return *this;
    }


    void print() const
    {
        cout << "id:" << _id << endl;
        cout << "age:" << _age << endl;
        cout << "name:" << _name << endl;
    }
private:
    int _id;
    int _age;
    char * _name;
};

void test0()
{
    cout << sizeof(Student) << endl;

    Student s1(100,20,"Tom");
    Student s2 = s1;
    s2.print();

    Student s3(101,21,"Jerry");
    s1 = s3;
    s1.print();
}

int main(void){
    test0();
    return 0;
}

